<?php //$Id: block_course_list.php,v 1.46.2.6 2008/08/29 04:23:38 isurumadushanka Exp $

require_once $CFG->libdir . '/ajax/ajaxlib.php';
                echo require_js(array('yui_dom-event','yui_animation', 'yui_container'));
include_once($CFG->dirroot . '/course/lib.php');

class block_course_list extends block_tree {
    function init() {
        $this->title = get_string('courses');
        $this->version = 2010102309;

        // Added by Isuru Madushanka
        // Indicates this specific tree class is for courses only.
        $this->course_specific = true;
    }
    
    function has_config() {
        return false;
    }

    
    function get_content() {
        global $THEME, $CFG, $USER;
        
        
        // Added by Isuru Madushanka
        // variables for storing icon files for completed and ongoing subjects.
        // CAUTION: valid image file must be used.
        $COMPLETED_COURSE_ICONFILE = 'tick_green_big.gif';
        $ONGOING_COURSE_ICONFILE = 'course.gif';

        // Added by Isuru Madushanka
        // variables for storing font color for completed and ongoing subjects.
        // all the colours are in hexadecimal format.
        $COMPLETED_COURSE_FONTCOLOR = '#777777';
        $ONGOING_COURSE_FONTCOLOR = '#000000';

        // sets our block instance as type of BLOCK_TYPE_TREE.
        $this->content_type = BLOCK_TYPE_TREE;
        
        if($this->content !== NULL) {
            return $this->content;
        }
        
        // define the icon path, font colot for currently conducting courses
        $icon  = "<img src=\"$CFG->pixpath/i/$ONGOING_COURSE_ICONFILE\" class=\"icon\" alt=\"".get_string("coursecategory")."\" />";
                 
        // define the font color for on-going courses.
        $font_color = "$ONGOING_COURSE_FONTCOLOR";       // default color will be theme color
        
        // define the icon path for completed courses... 
        // It is better to use two distinguishable icons for completed and non-completed.
        $icon_completed = "<img src=\"$CFG->pixpath/i/$COMPLETED_COURSE_ICONFILE\" class=\"icon\" alt=\"".get_string("coursecategory")."\" />";

        // define the font color for completed courses.
        $font_color_completed = " color=\"$COMPLETED_COURSE_FONTCOLOR\"";    // default is light grey color
        
        $adminseesall = true;
        if (isset($CFG->block_course_list_adminview)) {
           if ( $CFG->block_course_list_adminview == 'own'){
               $adminseesall = false;
           }
        }
        
        if ($this->content_type == BLOCK_TYPE_TREE) {

            // get all the courses that belongs to user who currently logged-in.
            // courses are retrived in Descending order of their course name and visibility.
            $courses = get_my_courses_forcoursetree($USER->id, 'visible DESC, fullname DESC');

                // iterate through each course...
                foreach ($courses as $course) {
                    if ($course->id == SITEID) {
                        continue;
                    }                  
                    
                    
                    $course_level = -1;
                    
                    if ($CFG->block_course_list_showtype == 1) {
                        
                        // Substract the level of the course by capturing nth character
                        // where an decimal digit first appears.
                        // from the short name of course
                        //    (i.e. module code CS3200 results $course_level = 3
                        //          UCS2980 results $course_level = 2).
                        // $course_level = substr($course->shortname, 2, 1);
                         for ($i = 0, $j = strlen($course->shortname); $i < $j; $i++) {
                            if (strstr('1234567890',$course->shortname[$i])) {
                                $course_level = $course->shortname[$i];
                                break;
                            } else if (strstr('-',$course->shortname[$i])) {
                                break;
                            } 
                         }
                         
                    } else {
                        // calculate the end date of the course
                        set_course_endtime($course);
                         
                        // course visibility is set according to the expiration of itself.
                        $expired = !$course->is_expired;
                        
                        if ($expired) 
                            $course_level = 1;
                        else 
                            $course_level = 0;
                    }
                    
                    //$course_level = getCourseLevel($course->shortname);
                    $contains = false;
                    // set current activated block to null;
                    $cur_block = NULL;
                    
                    // check whether course level is a numeric value.
                    // if it is not continue. You can do your own thing here if you want.
                    if (!is_numeric($course_level)) {
                        continue;
                    }
                    
                    $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
                    
                    
                    // check whether we are still in starting point...
                    $created_block = false;
                    if (empty($this->blocks) || !is_array($this->blocks)) {
                        // create a new instance of array for blocks
                        $this->blocks = array();
                        // create a new block instance. i.e. root node.
                        $cur_block = $this->blocks[] = new stdClass;
                        $created_block = true;
                        
                    }else {
                        
                        // finding for a existing block to add current course...
                        foreach ($this->blocks as $block) {
                            if ($block->id == $course_level) {
                                $cur_block = $block;
                                break;
                            }
                        }
                        
                        // if any block is not found which having name as course level...
                        if ($cur_block == NULL) {
                            $cur_block = $this->blocks[] = new stdClass;
                            $created_block = true;
                        }
                        
                    }
                    
                    if ($created_block == true) {
                        // sets the block id as course level
                        $cur_block->id = $course_level;
                        
                        
                        if ($CFG->block_course_list_showtype == 1) {
                            // name the title according to the course level value
                            if ($course_level >= $CFG->block_course_list_postgradstartlevel) {
                                $cur_block->title = get_string('nodeotherslabel', 'block_course_treelist');
                            } else if ($course_level < 0) {
                                $cur_block->title = get_string('nodeunknownlevel', 'block_course_treelist');
                            } else {
                                $cur_block->title = get_string('nodelabelprefix', 'block_course_treelist') . 
                                $cur_block->id . get_string('nodelabelpostfix', 'block_course_treelist');
                            }
                        } else {
                            
                            if ($course_level == 1) 
                                $cur_block->title = get_string('nodetextongoing', 'block_course_treelist');
                            else
                                $cur_block->title = get_string('nodetextcompleted', 'block_course_treelist');
                            
                        }
                        
                        // create list of items to store child nodes
                        $cur_block->items = array();
                        // stores the path to icon
                        $cur_block->icons = array();
                        // stores the status of visibility of nodes
                        $cur_block->visibility = array();
                        // stores the additional info if user wants
                        $cur_block->flags = array();
                        // stores number of recent activities since last log
                        $cur_block->recent_activites = array();
                        // if you have any footer regarding to this root node, please mention here.
                        $cur_block->footer = ' ';
                    }
                    
                    if ($CFG->block_course_list_showtype != 2) {
                        // calculate the end date of the course
                        set_course_endtime($course);
                    }
                    
                    // course visibility is set according to the expiration of itself.
                    $cur_block->visibility[] = !$course->is_expired;

                    // item flag is always set to 1. For the purpose of course filtering.
                    // do not change this value, because sorting may not take place otherwise.
                    $cur_block->flags[] = 1;
                    
                    // check for number of new recent activities
                    $recent_acts = 0;
                    //$recent_acts = get_number_of_recent_activities($course);
                    $tooltiptext = get_print_recent_activity($course, $recent_acts);
                    if ($tooltiptext != '') $tooltiptext = '<hr/>'.$tooltiptext;
                    $recent_text = "";
                    if ($CFG->block_course_list_showrecentactivities) {
                        if ($recent_acts > 0) $recent_text = ' (<b>' . $recent_acts . '</b>)';
                    }
                    
                    $cur_block->recent_activites[] = $recent_acts;
					$linkcss = " class=\"block_course_treelist_tooltip\"";

                    // set item and icon according to the expiration of course itself.
                    if ($course->is_expired) {  // if the course already expired

						
			$itemtext = "<a $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".
					"<span><em>".format_string($course->fullname)."</em>$tooltiptext</span>".
					"<font size=".$CFG->block_course_list_fontsizecompleted." $font_color_completed>" .
                                        format_string($course->fullname).$recent_text."</font></a>";
                    
                        $cur_block->items[] = $itemtext;

                        // If admin allows to use different icons, use so.
                        if ($CFG->block_course_list_icondistinguishable)
                            $cur_block->icons[] = $icon_completed;
                        else
                            $cur_block->icons[] = $icon;

                    } else {            // if the course is currently holding
                        
			$itemtext = "<a $linkcss href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".
					"<span><em>".format_string($course->fullname)."</em>$tooltiptext</span>".
					"<font size=2 $font_color>".
                                        format_string($course->fullname).$recent_text."</font></a>";

                        $cur_block->items[] = $itemtext;
                        $cur_block->icons[] = $icon;
                        
                    }
                    
                }
                
                $this->title = 'My Course List';
                /// If we can update any course of the view all isn't hidden, show the view all courses link
                if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM)) || empty($CFG->block_course_list_hideallcourseslink)) {
                    $curblock->footer = "<a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a> ...";
                }
                
                
                return;
        }
            
            

 
        if (empty($CFG->disablemycourses) and 
            !empty($USER->id) and 
            !(has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM)) and $adminseesall) and
            !isguest()) {    // Just print My Courses
            if ($courses = get_my_courses($USER->id, 'visible DESC, fullname ASC')) {
                foreach ($courses as $course) {
                    if ($course->id == SITEID) {
                        continue;
                    }
                    $linkcss = $course->visible ? "" : " class=\"dimmed\" ";
                    $this->content->items[]="<a $linkcss title=\"" . format_string($course->shortname) . "\" ".
                               "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">" . format_string($course->fullname) . "</a>";
                    $this->content->icons[]=$icon;
                }
                $this->title = get_string('mycourses');
            /// If we can update any course of the view all isn't hidden, show the view all courses link
                if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM)) || empty($CFG->block_course_list_hideallcourseslink)) {
                    $this->content->footer = "<a href=\"$CFG->wwwroot/course/index.php\">".get_string("fulllistofcourses")."</a> ...";
                }
            }
            $this->get_remote_courses();
            if ($this->content->items) { // make sure we don't return an empty list
                return $this->content;
            }
        }

        $categories = get_categories("0");  // Parent = 0   ie top-level categories only
        if ($categories) {   //Check we have categories
            if (count($categories) > 1 || (count($categories) == 1 && count_records('course') > 200)) {     // Just print top level category links
                foreach ($categories as $category) {
                    $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
                    $this->content->items[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">" . format_string($category->name) . "</a>";
                    $this->content->icons[]=$icon;
                }
            /// If we can update any course of the view all isn't hidden, show the view all courses link
                if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM)) || empty($CFG->block_course_list_hideallcourseslink)) {
                    $this->content->footer .= "<a href=\"$CFG->wwwroot/course/index.php\">".get_string('fulllistofcourses').'</a> ...';
                }
                $this->title = get_string('categories');
            } else {                          // Just print course names of single category
                $category = array_shift($categories);
                $courses = get_courses($category->id);

                if ($courses) {
                    foreach ($courses as $course) {
                        $linkcss = $course->visible ? "" : " class=\"dimmed\" ";

                        $this->content->items[]="<a $linkcss title=\""
                                   . format_string($course->shortname)."\" ".
                                   "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">" 
                                   .  format_string($course->fullname) . "</a>";
                        $this->content->icons[]=$icon;
                    }
                /// If we can update any course of the view all isn't hidden, show the view all courses link
                    if (has_capability('moodle/course:update', get_context_instance(CONTEXT_SYSTEM)) || empty($CFG->block_course_list_hideallcourseslink)) {
                        $this->content->footer .= "<a href=\"$CFG->wwwroot/course/index.php\">".get_string('fulllistofcourses').'</a> ...';
                    }
                    $this->get_remote_courses();
                } else {
                    
                    $this->content->icons[] = '';
                    $this->content->items[] = get_string('nocoursesyet');
                    if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $category->id))) {
                        $this->content->footer = '<a href="'.$CFG->wwwroot.'/course/edit.php?category='.$category->id.'">'.get_string("addnewcourse").'</a> ...';
                    }
                    $this->get_remote_courses();
                }
                $this->title = get_string('courses');
            }
        }

        return $this->content;
    }

/*
    function getCourseLevel($course_name) {
        for ($i = 0, $j = strlen($course_name); $i < $j; $i++) {
            echo $course_name[$i];
            if (strstr('1234567890',$course_name[$i])) {

                return $course_name[$i];
            }
        }
    }
*/

    function get_remote_courses() {
        global $THEME, $CFG, $USER;

        if (!is_enabled_auth('mnet')) {
            // no need to query anything remote related
            return;
        }

        $icon  = '<img src="'.$CFG->pixpath.'/i/mnethost.gif" class="icon" alt="'.get_string('course').'" />';

        // only for logged in users!
        if (!isloggedin() || isguest()) {
            return false;
        }

        if ($courses = get_my_remotecourses()) {
            $this->content->items[] = get_string('remotecourses','mnet');
            $this->content->icons[] = '';
            foreach ($courses as $course) {
                $this->content->items[]="<a title=\"" . format_string($course->shortname) . "\" ".
                    "href=\"{$CFG->wwwroot}/auth/mnet/jump.php?hostid={$course->hostid}&amp;wantsurl=/course/view.php?id={$course->remoteid}\">" 
                    . format_string($course->fullname) . "</a>";
                $this->content->icons[]=$icon;
            }
            // if we listed courses, we are done
            return true;
        }

        if ($hosts = get_my_remotehosts()) {
            $this->content->items[] = get_string('remotemoodles','mnet'); 
            $this->content->icons[] = '';
            foreach($USER->mnet_foreign_host_array as $somehost) {
                $this->content->items[] = $somehost['count'].get_string('courseson','mnet').'<a title="'.$somehost['name'].'" href="'.$somehost['url'].'">'.$somehost['name'].'</a>';
                $this->content->icons[] = $icon;
            }
            // if we listed hosts, done
            return true;
        }

        return false;
    }

    

}

?>
